// Copyright Epic Games, Inc. All Rights Reserved.

#include "SHotPakerCookPage.h"

#include "Framework/Commands/UIAction.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "Textures/SlateIcon.h"
#include "Widgets/SBoxPanel.h"
#include "Widgets/Input/SComboButton.h"
#include "Widgets/Text/STextBlock.h"

#include "Widgets/Cook/SHotPakerCookByTheBookSettings.h"
#include "Widgets/Cook/SHotPakerCookOnTheFlySettings.h"


#define LOCTEXT_NAMESPACE "SHotPakerCookPage"


/* SHotPakerCookPage structors
 *****************************************************************************/

SHotPakerCookPage::~SHotPakerCookPage()
{
}


/* SHotPakerCookPage interface
 *****************************************************************************/

void SHotPakerCookPage::Construct(const FArguments& InArgs, const TSharedRef<FHotPakerModel>& InModel)
{
	Model = InModel;

	// create cook modes menu
	FMenuBuilder CookModeMenuBuilder(true, NULL);
	{
		FUIAction ByTheBookAction(FExecuteAction::CreateSP(this, &SHotPakerCookPage::HandleCookModeMenuEntryClicked, ELauncherProfileCookModes::ByTheBook));
		CookModeMenuBuilder.AddMenuEntry(LOCTEXT("ByTheBookAction", "By the book"), LOCTEXT("ByTheBookActionHint", "Specify which content should be cooked and cook everything in advance prior to launching the game."), FSlateIcon(), ByTheBookAction);

		FUIAction OnTheFlyAction(FExecuteAction::CreateSP(this, &SHotPakerCookPage::HandleCookModeMenuEntryClicked, ELauncherProfileCookModes::OnTheFly));
		CookModeMenuBuilder.AddMenuEntry(LOCTEXT("OnTheFlyAction", "On the fly"), LOCTEXT("OnTheFlyActionHint", "Cook the content at run-time before it is being sent to the device."), FSlateIcon(), OnTheFlyAction);

		FUIAction DoNotCookAction(FExecuteAction::CreateSP(this, &SHotPakerCookPage::HandleCookModeMenuEntryClicked, ELauncherProfileCookModes::DoNotCook));
		CookModeMenuBuilder.AddMenuEntry(LOCTEXT("DoNotCookAction", "Do not cook"), LOCTEXT("DoNotCookActionHint", "Do not cook the content at this time."), FSlateIcon(), DoNotCookAction);
	}

	ChildSlot
		[
			SNew(SVerticalBox)

			+ SVerticalBox::Slot()
		.AutoHeight()
		[
			SNew(SHorizontalBox)

			+ SHorizontalBox::Slot()
		.FillWidth(1.0)
		.VAlign(VAlign_Center)
		[
			SNew(STextBlock)
			.Text(LOCTEXT("HowToCookText", "How would you like to cook the content?"))
		]

	+ SHorizontalBox::Slot()
		.AutoWidth()
		.Padding(8.0, 0.0, 0.0, 0.0)
		[
			// cooking mode menu
			SNew(SComboButton)
			.ButtonContent()
		[
			SNew(STextBlock)
			.Text(this, &SHotPakerCookPage::HandleCookModeComboButtonContentText)
		]
	.ContentPadding(FMargin(6.0, 2.0))
		.MenuContent()
		[
			CookModeMenuBuilder.MakeWidget()
		]
		]
		]

	+ SVerticalBox::Slot()
		.AutoHeight()
		.Padding(0.0, 8.0, 0.0, 0.0)
		[
			SNew(SHotPakerCookOnTheFlySettings, InModel)
			.Visibility(this, &SHotPakerCookPage::HandleCookOnTheFlySettingsVisibility)
		]

	+ SVerticalBox::Slot()
		.AutoHeight()
		.Padding(0.0, 8.0, 0.0, 0.0)
		[
			SNew(SHotPakerCookByTheBookSettings, InModel)
			.Visibility(this, &SHotPakerCookPage::HandleCookByTheBookSettingsVisibility)
		]
		];

}


/* SHotPakerCookPage callbacks
 *****************************************************************************/

EVisibility SHotPakerCookPage::HandleCookByTheBookSettingsVisibility() const
{
	ILauncherProfilePtr SelectedProfile = Model->GetHotPakerProfile();

	if (SelectedProfile.IsValid())
	{
		if (SelectedProfile->GetCookMode() == ELauncherProfileCookModes::ByTheBook)
		{
			return EVisibility::Visible;
		}
	}

	return EVisibility::Collapsed;
}


FText SHotPakerCookPage::HandleCookModeComboButtonContentText() const
{
	ILauncherProfilePtr SelectedProfile = Model->GetHotPakerProfile();

	if (SelectedProfile.IsValid())
	{
		ELauncherProfileCookModes::Type CookMode = SelectedProfile->GetCookMode();

		if (CookMode == ELauncherProfileCookModes::ByTheBook)
		{
			return LOCTEXT("CookModeComboButton_ByTheBook", "By the book");
		}

		if (CookMode == ELauncherProfileCookModes::DoNotCook)
		{
			return LOCTEXT("CookModeComboButton_DoNotCook", "Do not cook");
		}

		if (CookMode == ELauncherProfileCookModes::OnTheFly)
		{
			return LOCTEXT("CookModeComboButton_OnTheFly", "On the fly");
		}

		return LOCTEXT("CookModeComboButtonDefaultText", "Select...");
	}

	return FText();
}


void SHotPakerCookPage::HandleCookModeMenuEntryClicked(ELauncherProfileCookModes::Type CookMode)
{
	ILauncherProfilePtr SelectedProfile = Model->GetHotPakerProfile();

	if (SelectedProfile.IsValid())
	{
		SelectedProfile->SetCookMode(CookMode);
	}
}


EVisibility SHotPakerCookPage::HandleCookOnTheFlySettingsVisibility() const
{
	ILauncherProfilePtr SelectedProfile = Model->GetHotPakerProfile();

	if (SelectedProfile.IsValid())
	{
		if (SelectedProfile->GetCookMode() == ELauncherProfileCookModes::OnTheFly)
		{
			return EVisibility::Visible;
		}
	}

	return EVisibility::Collapsed;
}



#undef LOCTEXT_NAMESPACE
